<template>
	<u-upload :action="action" :file-list="fileList" max-count="1" :before-upload="beforeUpload" :show-progress="false"
		:multiple="false" width="140" height="140" @on-success="upSuccess" @on-progress="onProgress"
		@on-uploaded="onUploaded" :size-type="sizeType">
	</u-upload>
</template>

<script>
	let _this = {}
	export default {
		name: "fastdfs-upload",
		data() {
			return {
				action: "http://124.221.192.169:996/emos-wx-api/file/common/upload",
				upFileName: "",
				fileList: [],
	            sizeType:['compressed']
			};
		},
		created() {
			// wx小程序中this会在上传前钩子内丢失
			_this = this
			let fileInit = {}
			fileInit.url = this.vuex_user.avatar
			this.fileList[0] = fileInit
			console.log('created', this.fileList)
		},
		methods: {
			// 上传前的钩子
			async beforeUpload(index, list) {
				// 设置唯一文件名
				const {
					file
				} = list[0]
				// #ifdef H5
				const fileName = file.name
				//  #endif
				// 小程序拿的是path
				// #ifndef H5
				const fileName = file.path
				//  #endif
				const suffix = fileName.slice(fileName.lastIndexOf("."))
				const upFileName = _this.$u.guid(20) + suffix
				_this.upFileName = upFileName
				//console.log('fileName', fileName)
				return true
			},

			// 上传成功后发射事件给父组件 (带图片key参数)
			async upSuccess(lists) {
				console.log('upSuccess', lists.fullPath)
				this.$emit("upSuccess", lists.fullPath)
			},
			//全部上传完毕
			async onProgress(res) {
				//console.log('onProgress',res)
			},
			async onUploaded(lists, name) {
				//console.log('onUploaded',lists)
				//console.log('onUploaded',name)
			}
		},

	}
</script>

<style lang="scss">

</style>
